package com.zbw.printlibrary.printSub;

import android.content.Context;
import android.graphics.Bitmap;
import com.imin.printer.PrinterHelper;
import com.zbw.printlibrary.bean.AttributeBean;
import com.zbw.printlibrary.printConnector.IPrintCallBack;
import com.zbw.printlibrary.printConnector.IPrinterControl;
import com.zbw.printlibrary.util.PrintContentToBitmap;
import com.zbw.printlibrary.util.PrintThreadPoolManager;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者 ：Wesley
 * 时间 ：2025-03-25 17:39
 * 这个类是干嘛的？：PrintIMin
 * 一敏打印类
 */
public class PrintIMin implements IPrinterControl {
    private final Context mContext;
    private final IPrintCallBack mCallBack;
    private boolean isInit = false;

    public PrintIMin(Context context, IPrintCallBack callBack) {
        mContext = context;
        mCallBack = callBack;
    }

    @Override
    public void connectPrinter() {
        isInit = PrinterHelper.getInstance().initPrinterService(mContext);
        if(isInit){
            mCallBack.onConnect("连接成功",true);
        }else{
            mCallBack.onConnect("连接失败",false);
        }
    }

    @Override
    public void disConnectPrinter() {
        isInit = false;
        PrinterHelper.getInstance().deInitPrinterService(mContext);
    }

    @Override
    public void printTest(final List<String> testList) {
        PrintThreadPoolManager.getInstance().executeTask(new Runnable() {
            @Override
            public void run() {
                try {
                    for (int i = 0; i < testList.size(); i++) {
                        String context = testList.get(i);
                        if(i == 0) {
                            PrinterHelper.getInstance().setFontBold(true);
                            PrinterHelper.getInstance().setFontMultiple(2, 2);
                            PrinterHelper.getInstance().printTextWithAli(context + "\n", 1, null);
                        }else if(i == 1){
                            PrinterHelper.getInstance().setFontMultiple(1, 1);
                            PrinterHelper.getInstance().printTextWithAli(context + "\n", 1, null);
                        }else {
                            PrinterHelper.getInstance().setFontMultiple(0, 0);
                            PrinterHelper.getInstance().printTextWithAli(context + "\n", 0, null);
                        }
                    }
                    PrinterHelper.getInstance().printAndLineFeed();
                    PrinterHelper.getInstance().fullCut();
                    mCallBack.onPrintResult(true);
                } catch (Exception e) {
                    e.printStackTrace();
                    mCallBack.onPrintResult(false);
                }
            }
        });
    }

    @Override
    public void printString(final List<AttributeBean> testList) {
        PrintThreadPoolManager.getInstance().executeTask(new Runnable() {
            @Override
            public void run() {
                try {
                    for (int i = 0; i < testList.size(); i++) {
                        AttributeBean bean = testList.get(i);
                        if(bean.getContext_type() == 1){//图片
                            PrinterHelper.getInstance().printBitmapWithAlign(bean.getBitmap(),bean.getFont_gray(),null);
                        }else {
                            PrinterHelper.getInstance().setFontBold(bean.isBold());
                            PrinterHelper.getInstance().setFontMultiple(bean.getFont_size(),bean.getFont_size());
                            if(bean.isFont_line()){
                                PrinterHelper.getInstance().printTextWithAli(bean.getContext()+"\n",bean.getFont_gray(),null);
                            }else {
                                PrinterHelper.getInstance().printTextWithAli(bean.getContext(),bean.getFont_gray(),null);
                            }
                        }
                    }
                    PrinterHelper.getInstance().printAndLineFeed();
                    PrinterHelper.getInstance().fullCut();
                    mCallBack.onPrintResult(true);
                } catch (Exception e) {
                    e.printStackTrace();
                    mCallBack.onPrintResult(false);
                }
            }
        });
    }

    @Override
    public void printString(List<AttributeBean> testList, int width) {
        if(width == 80){
            PrinterHelper.getInstance().setPageFormat(80);
        }else if(width == 58){
            PrinterHelper.getInstance().setPageFormat(58);
        }
        List<List<AttributeBean>> allData = new ArrayList<>();
        List<AttributeBean> temList = null;
        for (int i = 0; i < testList.size(); i++) {
            if(temList == null){
                temList = new ArrayList<>();
            }
            temList.add(testList.get(i));
            if(testList.get(i).isFont_line()){
                allData.add(temList);
                temList = null;
            }
        }
        List<AttributeBean> data = new ArrayList<>();
        for (int i = 0; i < allData.size(); i++) {
            PrintContentToBitmap bt = new PrintContentToBitmap(allData.get(i),width);
            try {
                Bitmap bitmap = bt.Print();
                data.add(new AttributeBean(0,0,"",1,bitmap));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        printString(data);
    }

    @Override
    public void printStringAndBitmap(final List<String> testList, final LinkedHashMap<Integer, Bitmap> bitmapMap) {
        PrintThreadPoolManager.getInstance().executeTask(new Runnable() {
            @Override
            public void run() {
                try {
                    for (int i = 0; i < testList.size(); i++) {
                        String context = testList.get(i);
                        if(i == 0) {
                            PrinterHelper.getInstance().setFontBold(true);
                            PrinterHelper.getInstance().setFontMultiple(2, 2);
                            PrinterHelper.getInstance().printTextWithAli(context + "\n", 1, null);
                        }else if(i == 1 || context.startsWith("牌号")){
                            PrinterHelper.getInstance().setFontMultiple(1, 1);
                            PrinterHelper.getInstance().printTextWithAli(context + "\n", 1, null);
                        }else {
                            PrinterHelper.getInstance().setFontMultiple(0, 0);
                            PrinterHelper.getInstance().printTextWithAli(context + "\n", 0, null);
                        }
                        Bitmap bitmap = bitmapMap.get(i);
                        if (bitmap != null) {
                            PrinterHelper.getInstance().printBitmapWithAlign(bitmap,1,null);
                        }
                    }

                    if (!bitmapMap.isEmpty()) {
                        //传入的position位置大于list的话默认在最后面打印
                        for (Map.Entry<Integer, Bitmap> entry : bitmapMap.entrySet()) {
                            if (entry.getKey() > testList.size() - 1) {
                                Bitmap showBitmap = entry.getValue();
                                PrinterHelper.getInstance().printBitmapWithAlign(showBitmap,1,null);
                            }
                        }
                    }

                    PrinterHelper.getInstance().printAndLineFeed();
                    PrinterHelper.getInstance().fullCut();
                    mCallBack.onPrintResult(true);
                } catch (Exception e) {
                    e.printStackTrace();
                    mCallBack.onPrintResult(false);
                }
            }
        });
    }

    @Override
    public void blueConnect(String address) {

    }

    @Override
    public void disBlueConnect() {

    }

    @Override
    public void openMoneyBox() {
        PrinterHelper.getInstance().openDrawer();
    }

    @Override
    public boolean isConnected() {
        return isInit;
    }
}
